package emulator.hardware.clock;

public class TickHelper {

	private long[] handle_ticks = null;
	private int act_handles = 0;
	private long min_ticks = 0;
	private long max_ticks = 0;

	public TickHelper(long[] handle_ticks, int act_handles)
	{
		this.handle_ticks = handle_ticks;
		this.act_handles = act_handles;
		calcMinMax();
	}

	public void calcMinMax() {
		min_ticks = Long.MAX_VALUE;
		max_ticks = Long.MIN_VALUE;
		for (int i = 0; i < act_handles; i++)
		{
			if (handle_ticks[i] >= 0)
			{
				if (handle_ticks[i] < min_ticks)
					min_ticks = handle_ticks[i];
				if (handle_ticks[i] > max_ticks)
					max_ticks = handle_ticks[i];
			}
		}
	}
	
	public long getMin()
	{
		return min_ticks;
	}
	
	public long getMax()
	{
		return max_ticks;
	}

	public void rebaseTicks() {
		for (int i = 0; i < act_handles; i++)
		{
			if (handle_ticks[i] >= 0)
				handle_ticks[i] -= min_ticks;
		}
		max_ticks -= min_ticks;
		min_ticks = 0;
	}

	public void setHandleCount(int act_handles) {
		this.act_handles = act_handles;
		
	}
}
